package com.naxiangfish.cn.utils;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.naxiangfish.cn.R;
import com.naxiangfish.cn.backface.BackCall;
import com.naxiangfish.cn.weigit.CustomHorizontalProgresWithNum;

/**
 * 作者: 赵航
 * 创建时间: 2018/8/20 16:22
 * 描述:
 */
public class DialogUtil {
    /**
     * 两个按钮的dialog
     */
    public static Dialog showInfoDialog(final Context context, String titel, String content, String cancel, String confirm, final BackCall backCall){

        final Dialog dialog = new Dialog(context, R.style.dialog);
        //去除标题栏
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.updata_layout, null);
        TextView title = (TextView) layout.findViewById(R.id.title);
        title.setText(titel);
        TextView cont = (TextView) layout.findViewById(R.id.content);
        cont.setText(content);
        TextView conf = (TextView) layout.findViewById(R.id.confirm);
        conf.setText(confirm);
        TextView canc = (TextView) layout.findViewById(R.id.cancel);
        canc.setText(cancel);
        //点击确定
        conf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (backCall!=null){
                    backCall.deal(R.id.confirm,dialog);
                }
                dialog.dismiss();
            }
        });
        //点击取消
        canc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (backCall!=null){
                    backCall.deal(R.id.cancel,dialog);
                }
                dialog.dismiss();
            }
        });
        //将自定义布局设置进去
        Window w = dialog.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.CENTER;
        dialog.onWindowAttributesChanged(lp);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(layout);
        dialog.setCancelable(false);
        dialog.show();
        return dialog;
    }

    /**
     * 单个按钮的
     */
    public static Dialog showSingleDialog(final Context context, String titel, String content, String confirm, final BackCall backCall){

        final Dialog dialog = new Dialog(context, R.style.dialog);
        //去除标题栏
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.updata_single_layout, null);
        TextView title = (TextView) layout.findViewById(R.id.title);
        title.setText(titel);
        TextView cont = (TextView) layout.findViewById(R.id.content);
        cont.setText(content);
        TextView conf = (TextView) layout.findViewById(R.id.confirm);
        conf.setText(confirm);
        //点击确定
        conf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (backCall!=null){
                    backCall.deal(R.id.confirm,dialog);
                }
                dialog.dismiss();
            }
        });
        //将自定义布局设置进去
        Window w = dialog.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.CENTER;
        dialog.onWindowAttributesChanged(lp);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(layout);
        dialog.setCancelable(false);
        dialog.show();
        return dialog;
    }

    //当前已经是最新版本了
//    Dialog dialog = DialogUtil.showSingleDialog(MainActivity.this, "版本更新提示", "已经是最新的版本了", "退出", new BackCall() {
//        @Override
//        public void deal(int which, Object... objects) {
//            switch (which) {
//                case R.id.confirm:
//                    break;
//            }
//            super.deal(which, objects);
//        }
//    });
//        dialog.show();


    /**
     * 下在进度圈
     */
    public static Dialog showDownFileDialog(final Context context,BackCall backCall){

        final Dialog dialog = new Dialog(context, R.style.dialog);
        //去除标题栏
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.downfile_layout, null);
        CustomHorizontalProgresWithNum  horizontalProgress = (CustomHorizontalProgresWithNum)layout.findViewById(R.id.horizontalProgress);
        horizontalProgress.setProgress(0);
        horizontalProgress.setMax(100);
        backCall.deal(0,horizontalProgress);
        //将自定义布局设置进去
        Window w = dialog.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.CENTER;
        dialog.onWindowAttributesChanged(lp);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(layout);
        dialog.setCancelable(false);
        dialog.show();
        return dialog;
    }

    /**
     * 当前版本已经是最新的版本了
     */
    public static Dialog UpDataFanDialoge(final Context context, final BackCall backCall){
        final Dialog dialog = new Dialog(context, R.style.dialog);
        //去除标题栏
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.updata_single_layout, null);

        TextView conf = (TextView) layout.findViewById(R.id.confirm);


        TextView title = (TextView) layout.findViewById(R.id.title);
        title.setText("版本更新");
        TextView cont = (TextView) layout.findViewById(R.id.content);
        cont.setText("当前已经是最新版本了");
        //点击确定
        conf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (backCall!=null){
                    backCall.deal(R.id.confirm,dialog);
                }
                dialog.dismiss();
            }
        });
        //将自定义布局设置进去
        Window w = dialog.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.CENTER;
        dialog.onWindowAttributesChanged(lp);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(layout);
        dialog.setCancelable(false);
        dialog.show();
        return dialog;
    }
}
